package t1o1o;


import java.util.Scanner;

/**
 * 姓名： 张东升
 * 日期: 2025年04月2025/4/7日
 * 342 4521 521 4 21 3 4 21 4 0
 * 234 5 3 25 5236 34 65 23 34 0
 *
 */
public class Main {


    public static void main(String[] args) {
        // 创建链表1
        Scanner sc = new Scanner(System.in);
        Node h1 = new Node();
        Node h2 = new Node();
        int size1 = createLinkList(h1, sc);
        int size2 = createLinkList(h2, sc);
        sort(h1, true, size1);
        display(h1, size1);
        sort(h2, true, size2);
        display(h2, size2);
        // 合并
        linkToNewLink(h1, h2);
        size1 += size2;
        sort(h1, true, size1);
        display(h1, size1);
        display(new Node(), 0);
        display(new Node(), 0);
    }

    public static void linkToNewLink(Node h1, Node h2) {
        // 循环遍历第一个链表找到最后一个元素
        Node node = h1.next;
        while (node.next != null) {
            node = node.next;
        }
        //合并
        node.next = h2.next;

    }


    // 创建数组
    public static int createLinkList(Node head, Scanner scanner) {
        int count = 0;
        Node temp = head;
        int n = scanner.nextInt();
        while (n != 0) {
            temp.next = new Node(n, null);
            temp = temp.next;
            n = scanner.nextInt();
            count++;
        }
        return count;
    }


    public static void display(Node head, int count) {
        Node next = head.next;
        System.out.println("The length:" + count);
        System.out.print("The elements:");
        while (next != null) {
            System.out.print(next.data + " ");
            next = next.next;
        }
        System.out.println();
    }


    // 排序单链表，从小到大
    public static void sort(Node head, boolean isAsc, int linkListCount) {
        Node pre = head.next;
        Node next = pre.next;
        if (isAsc) {
            for (int i = 0; i < linkListCount; i++) {
                while (next != null) {
                    if (pre.data > next.data) {
                        int temp = pre.data;
                        pre.data = next.data;
                        next.data = temp;
                    }
                    pre = next;
                    next = next.next;
                }
                pre = head.next;
                next = pre.next;
                ;
            }
        } else {
            for (int i = 0; i < linkListCount; i++) {
                while (next != null) {
                    if (pre.data < next.data) {
                        int temp = pre.data;
                        pre.data = next.data;
                        next.data = temp;
                    }
                    pre = next;
                    next = next.next;
                }
                pre = head.next;
                next = pre.next;
                ;
            }
        }
    }
}


class Node {
    public int data;
    public Node next;

    public Node(int data, Node next) {
        this.data = data;
        this.next = next;
    }

    public Node() {
    }
}
